/*
 * Copyright 2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.seppiko.chart.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.seppiko.commons.utils.StringUtil;

/**
 * @author Leonard Woo
 */
public class StringUtils extends StringUtil {

  public static String urlTranscoding(String data, Charset oldEncoding, Charset newEncoding) {
    return transcoding(urlDecode(data, oldEncoding), oldEncoding, newEncoding);
  }

  public static String urlConvertUtf8ToIso88591(String data) {
    return urlTranscoding(data, StandardCharsets.UTF_8, StandardCharsets.ISO_8859_1);
  }

  public static String urlDecode(String data, Charset encoding) {
    return URLDecoder.decode(data, encoding);
  }

  public static String urlDecode(String data, String encodingName)
      throws UnsupportedEncodingException {
    return URLDecoder.decode(data, encodingName);
  }

  public static String urlDecodeWithUtf8(String data) {
    return urlDecode(data, StandardCharsets.UTF_8);
  }
}
